package com.raising.modules.websocket.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.framework.websocket.WsServer;
import com.raising.modules.cms.entity.CmsArticle;
import com.raising.modules.cms.entity.CmsCategory;
import com.raising.modules.cms.service.CmsArticleService;
import com.raising.modules.cms.service.CmsCategoryService;
import com.raising.modules.sys.entity.Organization;
import com.raising.modules.sys.service.OrganizationService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JDateUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;

/**
 * websocket相关api
 * @author GaoYuan
 * @date 2018/7/31 下午4:19
 */
@Controller
@RequestMapping("${adminPath}/ws/api")
public class WebSocketApiController extends BaseController {




    /**
     * 跳转聊天页面
     * @author GaoYuan
     * @date 2018/7/31 下午4:20
     */
    @GetMapping("/chat")
    public String chat() {
        return "";
    }

    /**
     * 查询所有讨论组
     * @author GaoYuan
     * @date 2018/7/31 下午4:20
     */
    @GetMapping("/groups")
    @ResponseBody
    public Object list() {
        return WsServer.rooms;
    }

    /**
     * 发送消息 至 指定讨论组
     * @author GaoYuan
     * @date 2018/7/31 下午4:20
     */
    @GetMapping("/msgToGroup")
    @ResponseBody
    public Object msgToGroup(String roomName, String msg) throws IOException{
        Map<String,Session> map = WsServer.rooms.get(roomName);
        //遍历map中的值
        for (Session session : map.values()) {
            session.getBasicRemote().sendText("系统管理员:" + msg);
        }
        return "ok";
    }

}
